package utilities;

import org.lwjgl.opengl.GL11;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Hashtable;


public class TextureDecoder {


    public static ArrayList<Image> LoadSprites(String directory,String extension, int count){
        BufferedImage img;
        int Pic_format;
        int Pic_width;
        int Pic_height;
        ArrayList<Image> spr = new ArrayList<Image>();
        ColorModel RGBA_model = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8,8,8,8}, true, false,
                ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);

        ColorModel RGB_model = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8,8,8,0}, false, false,
                ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
        WritableRaster raster;
        BufferedImage tmp;
        ByteBuffer texture;
        String dir = new File(".").getAbsolutePath() + directory;
        System.err.println("Reading textures from: " + dir);
        for (int i = 0; i < count; i++){
            try {
                img = ImageIO.read(new File(directory + "/" + i + extension ));
            }
            catch (IOException e) {
                System.err.println("ERROR WHILE OPENING FILE: "+ dir + "/" + i + extension);
                System.err.println(e.getLocalizedMessage());
                return null;
            }
             if(img.getColorModel().hasAlpha()){
             raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, img.getWidth(), img.getHeight(), 4, null);
             tmp = new BufferedImage(RGBA_model,raster,false,new Hashtable());
             Pic_format = GL11.GL_RGBA;
             }
            else {
             raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, img.getWidth(), img.getHeight(), 3, null);
             tmp = new BufferedImage(RGB_model,raster,false,new Hashtable());
             Pic_format = GL11.GL_RGB;
             }
            Graphics g = tmp.getGraphics();
            g.setColor(new Color(0f,0f,0f,0f));
            g.fillRect(0,0,img.getWidth(),img.getHeight());
            g.drawImage(img,0,0,null);
            byte [] dat = ((DataBufferByte)tmp.getRaster().getDataBuffer()).getData();
            texture = ByteBuffer.allocateDirect(dat.length);
            texture.order(ByteOrder.nativeOrder());
            texture.put(dat, 0, dat.length);
            texture.flip();

            Pic_height = img.getHeight();
            Pic_width = img.getWidth();
            spr.add(new Image(Pic_width, Pic_height, Pic_format, texture));
        }


      return spr;
    }

    public static Image LoadImage(String path){
        BufferedImage img;
        int Pic_format;
        int Pic_width;
        int Pic_height;
        ColorModel RGBA_model = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8,8,8,8}, true, false,
                ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);

        ColorModel RGB_model = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8,8,8,0}, false, false,
                ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
        WritableRaster raster;
        BufferedImage tmp;
        ByteBuffer texture;
        System.err.println("Reading image from: " + path);
            try {
                img = ImageIO.read(new File(path));
            }
            catch (IOException e) {
                System.err.println("ERROR WHILE OPENING FILE: " + path);
                System.err.println(e.getLocalizedMessage());
                return null;
            }
             if(img.getColorModel().hasAlpha()){
             raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, img.getWidth(), img.getHeight(), 4, null);
             tmp = new BufferedImage(RGBA_model,raster,false,new Hashtable());
             Pic_format = GL11.GL_RGBA;
             }
            else {
             raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, img.getWidth(), img.getHeight(), 3, null);
             tmp = new BufferedImage(RGB_model,raster,false,new Hashtable());
             Pic_format = GL11.GL_RGB;
             }
            Graphics g = tmp.getGraphics();
            g.setColor(new Color(0f,0f,0f,0f));
            g.fillRect(0,0,img.getWidth(),img.getHeight());
            g.drawImage(img,0,0,null);
            byte [] dat = ((DataBufferByte)tmp.getRaster().getDataBuffer()).getData();
            texture = ByteBuffer.allocateDirect(dat.length);
            texture.order(ByteOrder.nativeOrder());
            texture.put(dat, 0, dat.length);
            texture.flip();

            Pic_height = img.getHeight();
            Pic_width = img.getWidth();
            return new Image(Pic_width, Pic_height, Pic_format, texture);
    }

}
